{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "source": [
    "乐学偶得版权所有 lexueoude.com 公众号：乐学Fintech"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "ROC（ Rate of Change ）更多的是一种思考与处理问题的方式，绝对变化很多时候容易引起歧义，误解，所以，我们“自己与自己比较”"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     trade_date    ts_code   open   high    low  close  pre_close  change  \\\n",
      "0    2019-01-14  000001.SZ  10.22  10.25  10.07  10.11      10.20   -0.09   \n",
      "1    2019-01-15  000001.SZ  10.11  10.28  10.09  10.24      10.11    0.13   \n",
      "2    2019-01-16  000001.SZ  10.24  10.50  10.23  10.48      10.24    0.24   \n",
      "3    2019-01-17  000001.SZ  10.54  10.57  10.17  10.25      10.48   -0.23   \n",
      "4    2019-01-18  000001.SZ  10.34  10.49  10.28  10.43      10.25    0.18   \n",
      "5    2019-01-21  000001.SZ  10.34  10.47  10.32  10.34      10.43   -0.09   \n",
      "6    2019-01-22  000001.SZ  10.34  10.44  10.26  10.28      10.34   -0.06   \n",
      "7    2019-01-23  000001.SZ  10.29  10.47  10.29  10.35      10.28    0.07   \n",
      "8    2019-01-24  000001.SZ  10.40  10.55  10.37  10.52      10.35    0.17   \n",
      "9    2019-01-25  000001.SZ  10.56  11.04  10.55  11.00      10.52    0.48   \n",
      "10   2019-01-28  000001.SZ  11.04  11.14  10.88  10.94      11.00   -0.06   \n",
      "11   2019-01-29  000001.SZ  10.96  11.07  10.77  11.00      10.94    0.06   \n",
      "12   2019-01-30  000001.SZ  10.95  11.18  10.86  10.95      11.00   -0.05   \n",
      "13   2019-01-31  000001.SZ  10.98  11.20  10.94  11.10      10.95    0.15   \n",
      "14   2019-02-01  000001.SZ  11.20  11.25  10.96  11.20      11.10    0.10   \n",
      "15   2019-02-11  000001.SZ  11.08  11.21  10.97  11.21      11.20    0.01   \n",
      "16   2019-02-12  000001.SZ  11.20  11.31  11.03  11.19      11.21   -0.02   \n",
      "17   2019-02-13  000001.SZ  11.19  11.41  11.12  11.38      11.19    0.19   \n",
      "18   2019-02-14  000001.SZ  11.30  11.33  11.19  11.25      11.38   -0.13   \n",
      "19   2019-02-15  000001.SZ  11.23  11.23  10.90  10.95      11.25   -0.30   \n",
      "20   2019-02-18  000001.SZ  11.05  11.36  11.04  11.36      10.95    0.41   \n",
      "21   2019-02-19  000001.SZ  11.36  11.62  11.24  11.27      11.36   -0.09   \n",
      "22   2019-02-20  000001.SZ  11.33  11.53  11.25  11.41      11.27    0.14   \n",
      "23   2019-02-21  000001.SZ  11.38  11.55  11.28  11.36      11.41   -0.05   \n",
      "24   2019-02-22  000001.SZ  11.35  11.57  11.15  11.54      11.36    0.18   \n",
      "25   2019-02-25  000001.SZ  11.70  12.64  11.57  12.55      11.54    1.01   \n",
      "26   2019-02-26  000001.SZ  12.55  12.57  12.15  12.20      12.55   -0.35   \n",
      "27   2019-02-27  000001.SZ  12.24  12.66  12.15  12.40      12.20    0.20   \n",
      "28   2019-02-28  000001.SZ  12.44  12.61  12.27  12.36      12.40   -0.04   \n",
      "29   2019-03-01  000001.SZ  12.48  12.78  12.20  12.76      12.36    0.40   \n",
      "..          ...        ...    ...    ...    ...    ...        ...     ...   \n",
      "298  2020-04-08  000001.SZ  12.88  12.92  12.72  12.78      12.88   -0.10   \n",
      "299  2020-04-09  000001.SZ  12.88  12.89  12.72  12.74      12.78   -0.04   \n",
      "300  2020-04-10  000001.SZ  12.76  12.98  12.65  12.79      12.74    0.05   \n",
      "301  2020-04-13  000001.SZ  12.67  12.71  12.47  12.59      12.79   -0.20   \n",
      "302  2020-04-14  000001.SZ  12.65  12.86  12.57  12.86      12.59    0.27   \n",
      "303  2020-04-15  000001.SZ  12.86  12.93  12.78  12.87      12.86    0.01   \n",
      "304  2020-04-16  000001.SZ  12.79  12.79  12.54  12.68      12.87   -0.19   \n",
      "305  2020-04-17  000001.SZ  12.77  13.04  12.65  12.89      12.68    0.21   \n",
      "306  2020-04-20  000001.SZ  12.86  13.05  12.77  12.99      12.89    0.10   \n",
      "307  2020-04-21  000001.SZ  13.30  13.70  13.30  13.45      12.99    0.46   \n",
      "308  2020-04-22  000001.SZ  13.37  13.42  13.16  13.23      13.45   -0.22   \n",
      "309  2020-04-23  000001.SZ  13.23  13.31  13.11  13.23      13.23    0.00   \n",
      "310  2020-04-24  000001.SZ  13.17  13.28  13.11  13.24      13.23    0.01   \n",
      "311  2020-04-27  000001.SZ  13.30  13.64  13.25  13.50      13.24    0.26   \n",
      "312  2020-04-28  000001.SZ  13.45  13.56  13.27  13.52      13.50    0.02   \n",
      "313  2020-04-29  000001.SZ  13.48  14.10  13.45  14.02      13.52    0.50   \n",
      "314  2020-04-30  000001.SZ  14.02  14.32  13.88  13.93      14.02   -0.09   \n",
      "315  2020-05-06  000001.SZ  13.76  13.89  13.61  13.77      13.93   -0.16   \n",
      "316  2020-05-07  000001.SZ  13.76  13.76  13.60  13.69      13.77   -0.08   \n",
      "317  2020-05-08  000001.SZ  13.76  14.02  13.68  13.95      13.69    0.26   \n",
      "318  2020-05-11  000001.SZ  13.92  14.13  13.90  14.00      13.95    0.05   \n",
      "319  2020-05-12  000001.SZ  13.95  14.05  13.72  13.79      14.00   -0.21   \n",
      "320  2020-05-13  000001.SZ  13.75  13.78  13.53  13.63      13.79   -0.16   \n",
      "321  2020-05-14  000001.SZ  13.55  13.59  13.22  13.30      13.63   -0.33   \n",
      "322  2020-05-15  000001.SZ  13.39  13.43  13.14  13.23      13.30   -0.07   \n",
      "323  2020-05-18  000001.SZ  13.20  13.34  13.12  13.20      13.23   -0.03   \n",
      "324  2020-05-19  000001.SZ  13.41  13.45  13.27  13.36      13.20    0.16   \n",
      "325  2020-05-20  000001.SZ  13.38  13.62  13.27  13.51      13.36    0.15   \n",
      "326  2020-05-21  000001.SZ  13.52  13.57  13.36  13.40      13.51   -0.11   \n",
      "327  2020-05-22  000001.SZ  13.33  13.34  12.92  12.92      13.40   -0.48   \n",
      "\n",
      "     pct_chg         vol       amount        RSV          K          D  \\\n",
      "0    -0.8824   500443.59   507862.882  87.272727  87.272727  87.272727   \n",
      "1     1.2859   542160.55   553027.309  96.460177  92.785197  90.580209   \n",
      "2     2.3438   977699.37  1017495.116  98.437500  95.462604  92.892922   \n",
      "3    -2.1947   882811.92   910212.066  66.315789  83.355466  88.931210   \n",
      "4     1.7561   738793.26   769973.637  85.263158  84.087802  87.071892   \n",
      "5    -0.8629   659355.76   684465.646  73.563218  80.241977  84.576149   \n",
      "6    -0.5803   424413.57   438991.450  59.154930  72.775995  80.398241   \n",
      "7     0.6809   537876.63   559384.396  57.692308  67.543953  75.939505   \n",
      "8     1.6425   679240.88   711495.718  90.000000  75.229213  75.696418   \n",
      "9     4.5627  2108361.85  2299718.224  95.789474  82.203580  77.903750   \n",
      "10   -0.5455  1035909.80  1141853.833  79.381443  81.251865  79.032842   \n",
      "11    0.5484   826631.10   904847.854  85.567010  82.701419  80.265199   \n",
      "12   -0.4545   712001.01   785204.571  75.000000  80.121020  80.216891   \n",
      "13    1.3699   831622.75   922602.874  89.361702  83.211835  81.218637   \n",
      "14    0.9009  1014270.07  1127216.186  94.949495  87.133344  83.194719   \n",
      "15    0.0893   801936.63   889951.528  95.833333  90.037762  85.479211   \n",
      "16   -0.1784   959936.90  1071873.084  87.234043  89.102239  86.688114   \n",
      "17    1.6979  1137365.74  1280182.096  96.511628  91.573708  88.317748   \n",
      "18   -1.1424   869185.19   977818.097  75.000000  86.046645  87.560372   \n",
      "19   -2.6667  1100127.46  1213914.428  28.125000  66.733622  80.616034   \n",
      "20    3.7443  1255522.21  1408279.680  90.909091  74.793728  78.674876   \n",
      "21   -0.7923  1270882.04  1445281.382  51.388889  66.991072  74.779754   \n",
      "22    1.2422   832629.87   949430.819  70.833333  68.271940  72.610289   \n",
      "23   -0.4382   754246.48   860591.070  63.888889  66.810836  70.677023   \n",
      "24    1.5845  1184047.79  1343020.336  88.888889  74.170478  71.841555   \n",
      "25    8.7522  2824935.02  3415946.860  94.827586  81.056363  74.913238   \n",
      "26   -2.7888  2017544.93  2485673.356  74.712644  78.941753  76.256100   \n",
      "27    1.6393  1979013.88  2454404.411  85.227273  81.036951  77.849736   \n",
      "28   -0.3226  1113099.80  1383110.810  81.481481  81.185129  78.961542   \n",
      "29    3.2362  1712679.16  2137669.556  98.773006  87.047785  81.656970   \n",
      "..       ...         ...          ...        ...        ...        ...   \n",
      "298  -0.7764   528716.14   676604.872  27.710843  40.903345  46.958157   \n",
      "299  -0.3130   408553.77   522027.888  22.891566  34.899419  42.938578   \n",
      "300   0.3925   666674.95   853689.950  41.379310  37.059383  40.978846   \n",
      "301  -1.5637   446214.40   562008.050  18.181818  30.766861  37.574851   \n",
      "302   2.1446   686086.87   874856.562  59.090909  40.208211  38.452638   \n",
      "303   0.0778   656396.40   843649.273  78.431373  52.949265  43.284847   \n",
      "304  -1.4763   789154.98   997623.816  41.176471  49.025000  45.198231   \n",
      "305   1.6562  1331164.77  1713215.766  73.684211  57.244737  49.213733   \n",
      "306   0.7758   818455.83  1058524.019  89.655172  68.048215  55.491894   \n",
      "307   3.5412  2122448.34  2861879.086  79.674797  71.923742  60.969177   \n",
      "308  -1.6357  1032802.74  1368222.854  61.788618  68.545368  63.494574   \n",
      "309   0.0000   646989.63   855052.110  61.788618  66.293118  64.427422   \n",
      "310   0.0756   566001.61   747473.770  60.344828  64.310354  64.388399   \n",
      "311   1.9637   936829.90  1263809.737  82.758621  70.459776  66.412192   \n",
      "312   0.1481   771564.17  1038718.080  84.482759  75.134104  69.319496   \n",
      "313   3.6982  1108722.39  1541638.203  94.482759  81.583655  73.407549   \n",
      "314  -0.6419   819540.43  1155968.238  74.838710  79.335340  75.383479   \n",
      "315  -1.1486  1008998.02  1382727.481  54.545455  71.072045  73.946335   \n",
      "316  -0.5810   662749.23   904349.531  47.933884  63.359325  70.417331   \n",
      "317   1.8992   934781.70  1297924.588  69.421488  65.380046  68.738236   \n",
      "318   0.3584   612862.29   859156.594  73.553719  68.104603  68.527025   \n",
      "319  -1.5000   558511.14   772109.502  50.467290  62.225499  66.426516   \n",
      "320  -1.1603   640358.79   871062.043  34.285714  52.912237  61.921757   \n",
      "321  -2.4211   944672.09  1259440.848   7.272727  37.699067  53.847527   \n",
      "322  -0.5263   756794.47  1004313.267   7.627119  27.675084  45.123379   \n",
      "323  -0.2268   637208.57   843479.669   7.920792  21.090320  37.112360   \n",
      "324   1.2121   600368.64   801755.671  23.762376  21.981006  32.068575   \n",
      "325   1.1228   690851.07   929928.885  38.613861  27.525291  30.554147   \n",
      "326  -0.8142   552312.00   742797.057  27.722772  27.591118  29.566471   \n",
      "327  -3.5821   856237.33  1119433.491   0.000000  18.394079  25.842340   \n",
      "\n",
      "              J KDJ金叉死叉    ROC_30  \n",
      "0     87.272727     NaN       NaN  \n",
      "1     97.195173      金叉       NaN  \n",
      "2    100.601966     NaN       NaN  \n",
      "3     72.203977      死叉       NaN  \n",
      "4     78.119623     NaN       NaN  \n",
      "5     71.573634     NaN       NaN  \n",
      "6     57.531502     NaN       NaN  \n",
      "7     50.752848     NaN       NaN  \n",
      "8     74.294804     NaN       NaN  \n",
      "9     90.803238      金叉       NaN  \n",
      "10    85.689910     NaN       NaN  \n",
      "11    87.573858     NaN       NaN  \n",
      "12    79.929279      死叉       NaN  \n",
      "13    87.198231      金叉       NaN  \n",
      "14    95.010594     NaN       NaN  \n",
      "15    99.154864     NaN       NaN  \n",
      "16    93.930490     NaN       NaN  \n",
      "17    98.085627     NaN       NaN  \n",
      "18    83.019192      死叉       NaN  \n",
      "19    38.968799     NaN       NaN  \n",
      "20    67.031431     NaN       NaN  \n",
      "21    51.413708     NaN       NaN  \n",
      "22    59.595241     NaN       NaN  \n",
      "23    59.078461     NaN       NaN  \n",
      "24    78.828326      金叉       NaN  \n",
      "25    93.342612     NaN       NaN  \n",
      "26    84.313058     NaN       NaN  \n",
      "27    87.411380     NaN       NaN  \n",
      "28    85.632302     NaN       NaN  \n",
      "29    97.829414     NaN  0.262117  \n",
      "..          ...     ...       ...  \n",
      "298   28.793722     NaN -0.147432  \n",
      "299   18.821102     NaN -0.156850  \n",
      "300   29.220456     NaN -0.117931  \n",
      "301   17.150882     NaN -0.148749  \n",
      "302   43.719356      金叉 -0.126359  \n",
      "303   72.278100     NaN -0.123894  \n",
      "304   56.678538     NaN -0.176088  \n",
      "305   73.306744     NaN -0.142382  \n",
      "306   93.160859     NaN -0.101038  \n",
      "307   93.832874     NaN -0.088753  \n",
      "308   78.646956     NaN -0.099387  \n",
      "309   70.024510     NaN -0.098774  \n",
      "310   64.154265      死叉 -0.088154  \n",
      "311   78.554946      金叉 -0.018182  \n",
      "312   86.763320     NaN  0.008203  \n",
      "313   97.935868     NaN  0.103068  \n",
      "314   87.239062     NaN  0.139002  \n",
      "315   65.323466      死叉  0.099840  \n",
      "316   49.243312     NaN  0.126749  \n",
      "317   58.663665     NaN  0.106265  \n",
      "318   67.259760     NaN  0.087801  \n",
      "319   53.823464     NaN  0.055896  \n",
      "320   34.893199     NaN  0.036502  \n",
      "321    5.402148     NaN  0.027821  \n",
      "322   -7.221506     NaN  0.033594  \n",
      "323  -10.953759     NaN  0.024050  \n",
      "324    1.805867     NaN  0.030069  \n",
      "325   21.467579     NaN  0.071372  \n",
      "326   23.640413     NaN  0.040373  \n",
      "327    3.497556     NaN  0.010955  \n",
      "\n",
      "[328 rows x 17 columns]\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df= pd.read_csv(r'C:\\Users\\yons\\Desktop\\stock_database\\00001.csv',encoding = 'GB2312')\n",
    "df.head()\n",
    "def ROC(df,n,indicator):\n",
    "    a = df[indicator].diff(n-1)\n",
    "    b = df[indicator].shift(n-1)\n",
    "    ROC = pd.Series(a/b, name = 'ROC_'+str(n))\n",
    "    df = df.join(ROC)\n",
    "    return df\n",
    "\n",
    "\n",
    "df = ROC(df , 30,'close')\n",
    "\n",
    "print(df)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}